package com.wlyuan.open.controller.resolver;

import cn.hutool.core.util.DesensitizedUtil;
import com.wlyuan.open.api.IOpenClientService;
import com.wlyuan.open.constants.SmsType;
import com.wlyuan.open.controller.BaseController;
import com.wlyuan.open.dto.OpenSmsCodeDTO;
import lombok.RequiredArgsConstructor;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 */
@Service
@RequiredArgsConstructor
public class OpenSmsResolver extends BaseController {
    @DubboReference(check = false)
    private IOpenClientService openClientService;

    public void resolver(OpenSmsCodeDTO sms) {
        //绑定账号，需要根据账号获取手机号
        if (SmsType.isBind(sms.getType())) {
            sms.setMobile(openClientService.getUserMobile(sms.getAccount()));
        }
        //重置秘钥
        if (SmsType.isChangeAppSecret(sms.getType())) {
            sms.setMobile(getOpenMobile());
        }
        //不是注册和绑定，发送短信需要校验是否登录
        boolean isLogin = SmsType.isBind(sms.getType()) || SmsType.isRegister(sms.getType());
        if (!isLogin) {
            getAppId();
        }
    }

    public String resolverBind(OpenSmsCodeDTO sms) {
        var mobile = openClientService.getUserMobile(sms.getAccount());
        //绑定账号，需要根据账号获取手机号
        if (SmsType.isBind(sms.getType())) {
            sms.setMobile(mobile);
        }
        return DesensitizedUtil.mobilePhone(mobile);
    }
}
